﻿function cnVeryCalendar(){
var border_frame   = '#468DDD';
var border_inner   = '#f6f6f6';
var fore_frameCaption = '#ffffff';
var back_frameCaption = '#468DDD';
var fore_currentMonth = '#ff0000';
var back_weekName   = '#e9e9e9';
var fore_dayMouseOver = '#ff6600';
var back_dayMouseOver = '#eeeeee';
var font_cnChar   = ' font-family:宋体,sans-serif; font-size:12px;';
var font_numChar   = 'font-size:12px;';
var style_cell   = ' line-height:14px';
var today_decoration = " font-weight:bold; color:red;"
var __cnVeryCalendarContainer = document.getElementById("cnVeryCalendarContainer");

var reciever;
var today = new Date();
var y = today.getFullYear();
var m = today.getMonth() + 1;


this.daysInMonth = function(y, m){
	switch (m){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		return 31;
		case 4:
		case 6:
		case 9:
		case 11:
		return 30;
		case 2:
		if (y % 4 != 0){
			return 28;
			}
		if (y % 100 == 0){
			return y % 400 == 0 ? 29 : 28;
			}
		return 29;
		}
	}


this.generateCalendarTable = function(){
   var i;
   var j = new Date(y, m-1, 1).getDay();
   var k = this.daysInMonth(y, m);
   var body = '';

   body += "<table align='center' cellpadding='0' cellspacing='0'>";   
   body += "   <tr><td class='CalendarHeader'>";
   body += "    <table align='center' width='100%' cellpadding='0' cellspacing='0'>";
   body += "     <tr>";
   body += "      <td align='center'><a href='javascript:calendar.loadPreviousMonth()'><font>&lt;</font></a></td>";
   body += "      <td align='center'>";
   body += "        <table border='0' cellspacing='0' cellpadding='0'>";
   body += "          <tr><td class='CalendarYear'>"+ y +"</td><td class='CalendarB'>年</td><td class='CalendarMonth'>"+ m +"</td><td class='CalendarB'>月</td><tr>";
   body += "        </table>";
   body += "      <td align='center'><a href='javascript:calendar.loadNextMonth()'><font>&gt;</font></a></td>";
   body += "     </tr>";
   body += "    </table>";
   body += "   </td>";
   body += " </tr>";
   body += " <tr>";
   body += "   <td valign = 'top'>";

   body += "<table align='center' cellpadding='0' cellspacing='0' border='0' class='CalendarTbody'>";
   body += " <tr>";
   body += "   <td>日</td>";
   body += "   <td>一</td>";
   body += "   <td>二</td>";
   body += "   <td>三</td>";
   body += "   <td>四</td>";
   body += "   <td>五</td>";
   body += "   <td>六</td>";
   body += " </tr>";

   if (j != 0){
    body += "<tr>";
    body += ("<td style='" + style_cell + "' colspan='" + j + "'></td>");
   }
   for (i=1; i<=k; i++){
    if ((i+j) % 7 == 1){
     body += "<tr>";
    }

    body += "<td";
    body += " onmouseover=\"this.className='calendarOver'\"";
    body += " onmouseout=\"this.className='calendarOut'\"";
    if (y == today.getFullYear() && m == today.getMonth()+1 && i == today.getDate())
    {
     body += " style=\"cursor:pointer; " + today_decoration + "; " + font_numChar + style_cell + "\"";
    }
    else
    {
     body += " style='cursor:pointer; " + font_numChar + style_cell + "'";
    }
    body += "><a href=\"javascript:getDateValue("+y+","+m+","+i+")\">" + i + "</a></td>";

    if ((i+j) % 7 == 0)
    {
     body += ("</tr>");
    }
   }

   if ((i+j) % 7 != 0)
   {
    body += ("</tr>");
   }
   if (j < (36-k))
   {
    body += ("<tr><td colspan='7' style='" + style_cell + "'>&nbsp;</td></tr>");
   }
   if (j == 0 && k == 28)
   {
    body += ("<tr></tr>");
   }

   body += "</table>";

   body += "</td></tr></table>";

   return body;
}


this.loadPreviousYear = function()
{
   y--;
   __cnVeryCalendarContainer.innerHTML = this.generateCalendarTable();
}
this.loadNextYear = function()
{
   y++;
   __cnVeryCalendarContainer.innerHTML = this.generateCalendarTable();
}
this.loadPreviousMonth = function()
{
   m--;
   if (m < 1)
   {
    m = 12;
    y--;
   }
   __cnVeryCalendarContainer.innerHTML = this.generateCalendarTable();
}
this.loadNextMonth = function()
{
   m++;
   if (m > 12)
   {
    m = 1;
    y++;
   }
   __cnVeryCalendarContainer.innerHTML = this.generateCalendarTable();
}


this.getAbsolutePosition = function(element)
{
   var point = { x: element.offsetLeft, y: element.offsetTop };
   if (element.offsetParent)
   {
    var parentPoint = this.getAbsolutePosition(element.offsetParent);
    point.x += parentPoint.x;
    point.y += parentPoint.y;
   }
   return point;
};


this.setHook = function()
{
   y = today.getFullYear();
   m = today.getMonth() + 1;
   __cnVeryCalendarContainer.innerHTML = this.generateCalendarTable();
   __cnVeryCalendarContainer.style.display = '';
}


this.fadeOut = function()
{
   __cnVeryCalendarContainer.style.display = 'none';
}


this.setValue = function(date)
{
   reciever.value = date;
   this.fadeOut();
}
}

var calendar = new cnVeryCalendar();

